################################################################################-
# Replication File for Wratil, Wäckerle and Proksch: Government Rhetoric and the 
# Representation of Public Opinion in International Negotiations
#
# This script runs the analysis presented in Appendix H.2.
#
# Additionally, it produces the following graph:
# Table H2
# Figure H2
#
# The CMP dataset cannot be copied to the Dataverse (due to terms of use) and is therefore
# not included in the replication data. In order to recreate the data, please
# go to https://wratil.eu/data and rerun his script to create the file 
# 'parties_cmp2019_online_2.2.dta' and include it in the data folder.
# If you want to skip this process, you can set the command 're_run_data' below
# to "no" and just load the dataset we created in order to replicate the table and 
# figure.
################################################################################-

library(tidyverse)   #version 1.3.2
library(readstata13) #version 0.10.0
library(readxl)      #version 1.4.0
library(lme4)        #version 1.1-30
library(stargazer)   #version 5.2.3
library(interplot)   #version 0.2.3

#### Settings ####

re_run_data <- "no"

#### Set Up and Load Data ####

if(re_run_data == "yes"){
  load("data/pircher_farjam2021.RData")
  
  longAll <- longAll %>% 
    dplyr::select(Year,Date,variable,opposition,'Doc nr')
  
  rm(Cabinet2,centeroids,CPDSRelevant,eurobarometerYear,eurostatAll,total)
  
  #Load Eurobarometer public opinion data
  eb <- read.dta13("data/eb_responsiveness_apsr.dta")
  
  #Load and streamline CMP-ParlGov data
  cmp_eu <- read.dta13("data/parties_cmp2019_online_2.2.dta", nonint.factors = TRUE)
  cmp_eu$country <- as.character(cmp_eu$country)
  cmp_eu$country <- cmp_eu$country %>% 
    recode("Belgium" = "BE",
           "Austria" = "AT",
           "Czech Republic" = "CZ",
           "Denmark" = "DK",
           "Germany" = "DE",
           "Greece" = "EL",
           "Italy" = "IT",
           "Bulgaria" = "BG",
           "Croatia" = "HR",
           "Cyprus" = "CY",
           "Estonia" = "EE",
           "Finland" = "FI",
           "France" = "FR",
           "Hungary" = "HU",
           "Ireland" = "IE",
           "Latvia" = "LV",
           "Lithuania" = "LT",
           "Luxembourg" = "LU",
           "Malta" = "MT",
           "Netherlands" = "NL",
           "Poland" = "PL",
           "Portugal" = "PT",
           "Romania" = "RO",
           "Slovakia" = "SK",
           "Slovenia" = "SI",
           "Spain" = "ES",
           "Sweden" = "SE",
           "United Kingdom" = "UK") 
  
  #cmp_eu$vote_date <- as.character(cmp_eu$date)
  
  #Create date variable in Pircher and Farjam dataset and streamline other datasets
  aux <- strsplit(longAll$Date, ".", fixed = TRUE) 
  month <- unlist(lapply(aux, function(l) l[[2]]))
  day <- unlist(lapply(aux, function(l) l[[1]]))
  month <- sprintf("%02s", month)
  day <- sprintf("%02s", day)
  longAll$vote_date <- as.Date(paste(as.character(longAll$Year), month, day, sep="-"))
  eb$country <- str_to_upper(eb$country, locale = "en")
  longAll$country <- as.character(longAll$variable)
  table(eb$country)
  table(longAll$country)
  eb$country[eb$country == "BU"] <- "BG"
  #eb$vote_date <- as.character(eb$start_date)
  
  #Import budgetary data
  budget_balance <- read_excel("data/eu_budget_contributions_2016.xlsx",sheet = "Data")
  budget_balance$country <- toupper(budget_balance$government)
  budget_balance <- budget_balance %>% 
    rename(Year = year)
  
  #Import unemployment data
  unemployment <- read_excel("data/eurostat_unemployment_rate_2019.xls",sheet = "Ready to load")
  unemployment$country <- toupper(unemployment$government)
  unemployment <- unemployment %>% 
    rename(Year = year)
  
  #Import inflation data
  inflation <- read_excel("data/eurostat_inflation_hicp_2019.xls",sheet = "Ready to load")
  inflation$country <- toupper(inflation$government)
  inflation <- inflation %>% 
    rename(Year = year)
  
  #Join all remaining datasets
  longAll <- longAll %>% 
    left_join(eb %>% 
                dplyr::select(country,start_date,image_lag6m),
              by=c("country" = "country",
                   "vote_date" = "start_date"))
  
  longAll <- longAll %>% 
    left_join(cmp_eu %>% 
                dplyr::select(country,date,gov_eu_cmp_static,gov_lr_cmp_static),
              by=c("country" = "country",
                   "vote_date" = "date"))
  
  longAll <- longAll %>% 
    left_join(budget_balance %>% dplyr::select(Year,country,eu_cont_gdp))
  
  longAll <- longAll %>% 
    left_join(unemployment %>% dplyr::select(Year,country,unemployment))
  
  longAll <- longAll %>% 
    left_join(inflation %>% dplyr::select(Year,country,inflation))
  
  #Recode opposition votes and vote identifier
  longAll$oppositionFactor <- as.numeric(longAll$opposition)
  longAll$Doc <- longAll$`Doc nr`
  
  longAll_dat <- longAll %>% filter(complete.cases(.))
  
  save(file="generated_data/appendix_h2_data.RData",longAll_dat)
}else{
  load(file="generated_data/appendix_h2_data.RData")
}

#Run model 1: Eurosceptic governments * Year
longAll_dat$Year <- longAll_dat$Year - 2010
longAll_dat$Vote_Date_num <- scale(longAll_dat$vote_date - min(longAll_dat$vote_date))
mod1 <- glmer(oppositionFactor ~ gov_eu_cmp_static+image_lag6m+
                  gov_lr_cmp_static+eu_cont_gdp+
                  unemployment+inflation+
                  (1|Doc) + (1|country), data = longAll_dat, 
                family = binomial(link = "logit"),
                control = glmerControl(optimizer = "bobyqa"),
                nAGQ = 1)

mod2 <- glmer(oppositionFactor ~ gov_eu_cmp_static*Vote_Date_num+image_lag6m+
                gov_lr_cmp_static+eu_cont_gdp+
                unemployment+inflation+
                (1|Doc) + (1|country), data = longAll_dat, 
              family = binomial(link = "logit"),
              control = glmerControl(optimizer = "bobyqa"),
              nAGQ = 1)

stargazer(mod1,mod2,type="text")

# This is Table H2
stargazer(mod1,mod2,
          align=TRUE,
          dep.var.labels   = "Opposition Voting",
          covariate.labels = c("Government EU position", "Date of Vote",
                               "Public image of the EU", 
                               "Government left-right position", 
                               "Net receipts from EU budget",
                               "Unemployment rate","Inflation rate",
                               "Government EU position x Date of Vote",
                               "Intercept"),
          omit.stat = c("f","ll","ser"),
          intercept.bottom = TRUE)

plot.dat <- interplot(mod2,
          var1 = "gov_eu_cmp_static",
          var2 = "Vote_Date_num",)

plot.dat <- plot.dat$data

plot.dat$date <- as.Date(seq.Date(from = as.Date(min(longAll_dat$vote_date)),length.out = 100,
                          to = as.Date(max(longAll_dat$vote_date))))

p.vote <- plot.dat %>% 
  ggplot(aes(x=date,y=coef1))+
  geom_smooth(se=F,col="black",size=0.5)+
  geom_smooth(aes(y=ub),se=F,col="black",size=0.5,lty="dashed")+
  geom_smooth(aes(y=lb),se=F,col="black",size=0.5,lty="dashed")+
  theme_bw()+
  labs(x="Date of Vote",y="Marginal effect of government EU position \non opposition vote")+
  theme(axis.title = element_text(face="bold"))

# This is Figure H2
ggsave(plot=p.vote,
       "figures_appendix/figure_h_2.eps", width = 6, height = 4, units = "in")
